import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../components/Login.vue";
import Home from "../components/Home.vue";
import Welcome from "../components/welcome.vue";
import Users from "../components/user/Users.vue";

Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/xxx",
      component: () => import("../components/xxx.vue"),
      // children: [
      //   {
      //     path: "/user",
      //     component: () => import("../components/user/user.vue"),
      //   },
      //   {
      //     path: "/roles",
      //     component: () => import("../components/Roles/Roles.vue"),
      //   },
      // ],
    },
    {
      path: "/login",
      component: Login,
    },
    {
      path: "/home",
      component: Home,
      redirect: "/welcome",
      children: [
        {
          path: "/welcome",
          component: Welcome,
        },
        {
          path: "/users",
          component: Users,
        },
        {
          path: "/roles",
          component: () => import("../components/Roles/Roles.vue"),
        },
        {
          path: "/rights",
          component: () => import("../components/Roles/Rights.vue"),
        },
        {
          path: "/goods",
          component: () => import("../components/shangping/goods.vue"),
        },
        {
          path: "/add",
          component:()=> import("../components/shangping/add.vue")
        },
        {
          path: "/params",
          component: () => import("../components/shangping/params.vue"),
        },
        {
          path: "/categories",
          component: () => import("../components/shangping/categories.vue"),
        },
        {
          path: "/orders",
          component: () => import("../components/Orders/orders.vue"),
        },
        {
          path: "/reports",
          component: () => import("../components/Reports/reports.vue"),
        },

      ],
    },
  ],
});

//路由重复
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

//掛載路由導航守衛
router.beforeEach((to, from, next) => {
  //to 將要訪問的路徑   from  從哪裡來的   next 放行
  if (to.path === "/login") return next();
  //獲取token
  const tokenStr = window.sessionStorage.getItem("token");
  // console.log(tokenStr);
  if (!tokenStr) return next("/login");
  console.log(123);

  next();
});

export default router;
